<?php
/**
 * Meishi_DAO_AirportCode
 * 机场信息
 *
 * 作者:  胡毅 (huyi@comsenz.com)
 * 创建时间: 2012-10-9 下午3:12:36
 * 修改记录:
 *
 * $id$
 */

class Flight_DAO_AirportCode extends DAO_MySql {

    /**
     * 状态：正常
     */
    const STATUS_NORMAL = 1;

    /**
     * 状态：删除
     */
    const STATUS_REMOVE = 9;

    /**
     * 要操作的表名
     *
     * @var string
     */
    private static $_table = 'flight_airport_codes';

    /**
     * 主键
     *
     * @var string
     */
    private static $_pk = 'threeCode';

    /**
     * 允许的字段
     *
     * @var array
     */
    private static $_allowed = array('threeCode',  'fourCode', 'acAirpotName', 'acAirpotEnName',
                                     'acCityName', 'acCountryName', 'acCountryCode', 'acCityPinyin',
                                     'acCityEnName', 'acCreated', 'acUpdated', 'acDeleted',
                                     'acStatus',);

    /**
     * 必须的字段
     *
     * @var array
    */
    private static $_required = array();

    /**
     * add
     * 添加一条记录
     *
     * @param array $data 数据信息
     * @param array $shardKey 分库分表关键字
     * @throws DAO_Exception
     * @return boolean
    */
    public static function add($data, $shardKey = array()) {

        if (!$data['acCreated']) {
            $data['acCreated'] = date('Y-m-d H:i:s');
        }

        if (!$data['acStatus']) {
            $data['acStatus'] = self::STATUS_NORMAL;
        }

        return parent::add(self::$_table, $data, self::$_required, self::$_allowed, $shardKey);
    }

    /**
     * update
     * 更新一条记录
     *
     * @param string $threeCode 机场三字码
     * @param array $data 数据信息
     * @param array $shardKey 分库分表关键字
     * @return boolean
     */
    public static function update($threeCode, $data, $shardKey = array() ) {

        if (!$data['acUpdated']) {
            $data['acUpdated'] = date('Y-m-d H:i:s');
        }

        return parent::update(self::$_table, $data, self::$_pk, $threeCode, self::$_allowed, $shardKey);
    }


    /**
     * remove
     * 删除一条记录
     *
     * @param string $threeCode
     * @param array $shardKey 分库分表关键字
     * @return boolean
     */
    public static function remove($threeCode, $shardKey = array()) {

        $data = array('acStatus' => self::STATUS_REMOVE,
                      'acDeleted' => date('Y-m-d H:i:s')
                     );

        return self::update($threeCode, $data, $shardKey);
    }

    /**
     * get
     * 获取一条记录
     *
     * @param string $threeCode
     * @param array   $fields 字段名
     * @param array $shardKey 分库关键字
     * @throws DAO_Exception
     * @return array
     */
    public static function get($threeCode, $fields = array(), $shardKey = array()) {

        if ($fields) {
            $result = array_diff($fields, self::$_allowed);
            if ($result) {
                throw new InvalidArgumentException("Invalid Field(" . join(',', $result) . ")");
            }
        } else {
            $fields = self::$_allowed;
        }

        $conditions = self::$_pk . ' = ? AND acStatus != ?';
        $params = array($threeCode, self::STATUS_REMOVE);

        return parent::find(self::$_table, $conditions, $params, $fields, $shardKey);
    }
}